package cn.itcast.bos.web.action.auth;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.auth.Role;
import cn.itcast.bos.web.action.base.BaseAction;

@Controller("roleAction")
@Scope("prototype")
@Namespace("/auth")
@ParentPackage("bos")
public class RoleAction extends BaseAction<Role> {
	// ids
	private String functionIds;

	public void setFunctionIds(String functionIds) {
		this.functionIds = functionIds;
	}

	@Action(value = "roleAction_save", results = { @Result(location = "/WEB-INF/pages/admin/role.jsp", name = "save") })
	public String save() {
		roleService.save(model, functionIds);
		return "save";
	}

	// 分页查询
	@Action(value = "roleAction_ajaxList")
	public String ajaxList() {
		List<Role> roles = roleService.findAllRoles();
		getValueStack().push(roles);
		return SUCCESS;
	}

	// 分页查询
	@Action(value = "roleAction_pageQuery")
	public String pageQuery() {
		Map<String, Object> results = new HashMap<String, Object>();
		// 1: Pageable 接口封装 datagrid 请求 page rows 当前页面和每页记录数
		Pageable pageRequest = new PageRequest(page - 1, rows);
		// 业务将调用spring-data接口实现 完成分页查询 获取分页数据返回值 Page对象
		Page<Role> pageData = roleService.findAll(pageRequest);
		// Page [{},{},{}] List<T> 实现Itrerable 接口 json 序列化 数组 [{},{}]
		results.put("total", pageData.getTotalElements());// map 封装 easyui datagrid 插件需要数据格式 total 和 rows
		results.put("rows", pageData.getContent());
		getValueStack().push(results);
		return SUCCESS;
	}

}
